import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/model/material_requisition_model.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';

import '../../../../../http/bean/works_2023/purorderInfo_bean.dart';
import '../../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../../http/bean/wroks/dispatchingaudit_bean.dart';
import '../../../../../http/bean/ywx_limit.dart';
import '../../../../../utils/a_combined_utils.dart';
import '../../../../app_ui.dart';

///物料信息
class UiPurorderAuditForm {
  static Widget build(
      BuildContext context, PurOrderInfoBean? materialInfo, YwxItem item,
      {String? title}) {
    return StatefulBuilder(builder: (context, setState) {
      List<Widget> children = [];
      if (StringUtils.isEmpty(title)) {
        title = AppStrings.of(context).MaterialInfo; //物料信息
      }
      children.add(UiBase.title(context, title,
          bottom: AppEdgeInsets.margin_bottom)); //标题
      children.add(_table(context, materialInfo!, item));
      var column = UiBase.column(context, children);
      var card = UiCard.build(context, column);
      return card;
    });
  }

  ///fixme Table表格里的行TabRow;每一行的表格数量必须相等。不然报错。
  ///fixme table嵌套table实现单元格合并。

  //表格线条颜色
  static Color _BorderColor = Colors.black12;

  //表格线条宽度
  static double _width = 1;

  ///背景色表格标题
  static Color _backgroundColorWithTableTitle = Colors.black12;
  static Color _backgroundColorWithWhite = Colors.white;
  static Color _backgroundColorWithGrey = ColorUtils.parseColor("#F7F6F3");
  static StateSetter? _setState = null;

  static Widget _table(
      BuildContext context, PurOrderInfoBean materialInfo, YwxItem item) {
    if (materialInfo?.PurOrderList == null ||
        materialInfo!.PurOrderList!.length <= 0) {
      return LibUi.getEmptyView();
    }
    //fixme 表格
    return StatefulBuilder(builder: (context, setState) {
      _setState = setState;
      //可以水平滑动的表格
      var scrolltable = _scrolltable(context, materialInfo, item);
      //水平滑动效果
      var singleChildScrollView = SingleChildScrollView(
          child: scrolltable, scrollDirection: Axis.horizontal);
      return Center(
        child: Container(
          child: singleChildScrollView,
          //width: 2400,
        ),
      );
    });
  }

  ///水平滑动表格
  static Widget _scrolltable(
      BuildContext context, PurOrderInfoBean materialInfo, YwxItem item) {
    if (materialInfo.PurOrderList == null ||
        materialInfo.PurOrderList!.length <= 0) {
      return AppUi.getEmptyView();
    }
    List<PurOrderListBean> datas = materialInfo.PurOrderList!;
    var isOrderType1 = materialInfo.isOrderType1(); //true 大额采购，false 零星采购
    var firstData = datas[0];
    var isMT01 = firstData.isMT01(); //是否为原片
    var isSourceType = false; //是否显示，来源类型
    var isPrice = false; //是否显示单价
    var isAmount = false; //是否显示金额
    var isQualityGrade = false; //是否显示质量等级
    if (isMT01 && (!isOrderType1)) {
      isQualityGrade = true; //原片，零星采购才有
    }
    if (isOrderType1) {
      //大额采购
      isSourceType = false;
      isPrice = false;
      isAmount = false;
    } else {
      //零星采购
      isSourceType = true;
      isPrice = true;
      isAmount = true;
    }
    var children = <TableRow>[];
    //第一行标题栏
    List<Widget> titleChildren = [];
    titleChildren.add(_text(AppStrings.of(context).TheSerial)); //序号
    titleChildren.add(_text(AppStrings.of(context).MaterialName3)); //物料名称
    titleChildren.add(_text(AppStrings.of(context).TypeOfMaterial)); //物料类型
    titleChildren.add(_text(AppStrings.of(context).MaterialCode)); //物料编码
    titleChildren.add(_text(AppStrings.of(context).Width_Height)); //规格型号
    if (firstData.Iswidth2()) {
      titleChildren.add(_text(AppStrings.of(context).ItemWidth)); //宽
    }
    if (firstData.Isheight2()) {
      titleChildren.add(_text(AppStrings.of(context).ItemHeight)); //高
    }
    if (firstData.Islength2()) {
      titleChildren.add(_text(AppStrings.of(context).long)); //长
    }
    if (firstData.Isthickness2()) {
      titleChildren.add(_text(AppStrings.of(context).thickness2)); //厚
    }
    if (firstData.Isquantity2()) {
      titleChildren.add(_text(AppStrings.of(context).ItemNum)); //数量
    }
    if (isSourceType) {
      titleChildren.add(_text(AppStrings.of(context).SourceType)); //来源类型
    }
    titleChildren
        .add(_text(AppStrings.of(context).PDMinNum, isStart: false)); //请购数量
    if (!firstData.isMT01()) {
      //辅料，五金；显示；包数量，和每包数量。
      titleChildren
          .add(_text(AppStrings.of(context).PackageCount, isStart: true)); //包数量
      titleChildren.add(
          _text(AppStrings.of(context).PackageListCount, isStart: true)); //每包数量
    }
    titleChildren.add(_text(AppStrings.of(context).NumberOfApplications,
        isStart: true)); //申请数量*
    titleChildren.add(_text(AppStrings.of(context).QuantityPurchased,
        isStart: false)); //已采购数量
    if (firstData.isSubsidiaryNum()) {
      titleChildren.add(_text(AppStrings.of(context).Area)); //面积
    }
    if (isPrice) {
      titleChildren
          .add(_text(AppStrings.of(context).Price2, isStart: true)); //单价
    }
    if (isAmount) {
      titleChildren.add(_text(AppStrings.of(context).DAmount)); //金额
    }
    if (isQualityGrade) {
      titleChildren.add(_text(AppStrings.of(context).QualityGrade2)); //质量等级
    }
    if (firstData.isMT01()) {
      titleChildren.add(_text(AppStrings.of(context).ManufacturerName)); //品牌
    } else {
      titleChildren.add(_text(AppStrings.of(context).ManufacturerName,
          isStart: true)); //品牌(带星)
    }
    titleChildren.add(_text(AppStrings.of(context).CurrentInventory)); //当前库存
    titleChildren.add(_text(AppStrings.of(context).Note)); //备注
    titleChildren.add(_text(AppStrings.of(context).DemandDeptName)); //请购部门
    titleChildren.add(_text(AppStrings.of(context).DemandPersonName)); //请购人
    titleChildren.add(_text(AppStrings.of(context).Requisitionlog)); //请购日志
    var tableRow = TableRow(
        decoration: BoxDecoration(color: _backgroundColorWithTableTitle),
        children: titleChildren);
    children.add(tableRow);
    //第二行，内容行。
    for (int i = 0; i < datas.length; i++) {
      var data = datas[i];
      List<Widget> rowChildren = [];
      //序号
      var TheSerial = _text("${i + 1}");
      rowChildren.add(TheSerial);
      //物料名称
      var MaterialName = _text("${data.MaterialName}");
      rowChildren.add(MaterialName);
      //物料类型
      var MaterialType = _text("${data.getMaterialTypeName(context)}");
      rowChildren.add(MaterialType);
      //物料编码
      var MaterialCode = _text("${data.MaterialCode}");
      rowChildren.add(MaterialCode);
      //规格，不可填写
      var Width_Height = StatefulBuilder(
        builder: (contxet, setState) {
          if (firstData.isMT01()) {
            var Size = "${data?.Width}*${data?.Height}";
            return _text(Size);
          }
          return _text(data?.Size);
        },
      );
      rowChildren.add(Width_Height);
      if (firstData.Iswidth2()) {
        //宽
        var ItemWidth = _text("${data?.getWidthInput()}${data?.WidthUnit}");
        rowChildren.add(ItemWidth);
      }
      if (firstData.Isheight2()) {
        //高
        // var ItemHeight = _text(StringUtils.toStringAsDecimal(data?.Height));
        var ItemHeight = _text("${data?.getHeightInput()}${data?.HeightUnit}");
        rowChildren.add(ItemHeight);
      }
      if (firstData.Islength2()) {
        //长
        // var long = _text(StringUtils.toStringAsDecimal(data?.Lengths));
        var long = _text("${data?.getLengthsInput()}${data?.LengthUnit}");
        rowChildren.add(long);
      }
      if (firstData.Isthickness2()) {
        //厚
        // var thickness = _text(StringUtils.toStringAsDecimal(data?.Thickness));
        var thickness =
            _text("${data?.getThicknessInput()}${data?.ThicknessUnit}");
        rowChildren.add(thickness);
      }
      if (firstData.Isquantity2()) {
        //数量
        // var ItemNum = _text(StringUtils.toStringAsDecimal(data?.Numbers));
        var ItemNum = _text(data?.getNumbersStrAndUnit());
        rowChildren.add(ItemNum);
      }
      if (isSourceType) {
        ///来源类型
        var SourceType = StatefulBuilder(
          builder: (contxet, setState) {
            return _text(data?.SourceType);
          },
        );
        rowChildren.add(SourceType);
      }
      //请购数量
      var PDMinNum = StatefulBuilder(
        builder: (contxet, setState) {
          return UiBase.itemWithSigleEdit(
            context,
            StringUtils.toStringAsDecimal(data?.PDMinNum),
            isAllowEmpty: true,
            isDecimal: true,
            readOnly: true,
            isNegative: false,
          );
        },
      );
      rowChildren.add(PDMinNum);
      if (!firstData.isMT01()) {
        //fixme 辅料，五金；显示；包数量，和每包数量。
        //包数量
        var PackageCount = StatefulBuilder(builder: (contxet, setState) {
          // PackageCountSetState = setState;
          // TextEditingController? _controller;
          // var _key = "PackageCount:${data?.ListId}${data?.index}_${i}";
          var PackageCount = UiBase.itemWithSigleEdit(
              context, StringUtils.toStringAsDecimal(data?.PackageCount),
              unit: data?.PackageUnit,
              readOnly: true,
              // onController: (value) {
              //   _controller = value;
              // },
              // mtextSelection: getTextSelection(_key),
              // focusNode: FocusNodeManager.getFocusNode(_key).addListener(
              //       (value) {
              //     if (value) {
              //       //聚焦
              //       setTextSelection(_key, _controller?.selection); //记录焦点位置
              //     } else {
              //       //失去焦点
              //       setTextSelection(_key, null);
              //     }
              //   },
              // ),
              isAllowEmpty: true,
              isDecimal: true,
              isNegative: false,
              isLine: true, onChanged: (value) {
            if (StringUtils.isEmpty(value)) {
              data?.PackageCount = "0"; //不能为空，为空，提交接口会报数据格式错误。
            } else {
              data?.PackageCount = value;
            }
            // isDemandNumUpdate = false;
            // data?.DemandNum = DecimalUtils.multiply(
            //     data?.toDouble(data?.PackageCount).toString(),
            //     data
            //         ?.toDouble(data?.PackageListCount)
            //         .toString()); //fixme 申请数量就等于包数量*每包数量
            // data?.MinNum = data?.DemandNum;
            // NumberOfApplications?.call(() {}); //申请数量
            ///sizeSetState?.call(() {}); //规格更新
            // getStockNum(
            //     context, CurrentInventorySetState, MaterialCodeSetState, data);

            ///库存数量
          });
          return PackageCount;
        });
        rowChildren.add(PackageCount);

        //每包数量
        var PackageListCount = StatefulBuilder(builder: (contxet, setState) {
          // PackageListCountSetState = setState;
          // TextEditingController? _controller;
          // var _key = "PackageListCount:${data?.ListId}${data?.index}_${i}";
          var _unit = "${data?.MainUnit}/${data?.PackageUnit}";
          var PackageListCount = UiBase.itemWithSigleEdit(
              context, StringUtils.toStringAsDecimal(data?.PackageListCount),
              unit: _unit,
              readOnly: true,
              // onController: (value) {
              //   _controller = value;
              // },
              // mtextSelection: getTextSelection(_key),
              // focusNode: FocusNodeManager.getFocusNode(_key).addListener(
              //       (value) {
              //     if (value) {
              //       //聚焦
              //       setTextSelection(_key, _controller?.selection); //记录焦点位置
              //     } else {
              //       //失去焦点
              //       setTextSelection(_key, null);
              //     }
              //   },
              // ),
              isAllowEmpty: true,
              isDecimal: true,
              isNegative: false,
              isLine: true, onChanged: (value) {
            if (StringUtils.isEmpty(value)) {
              data?.PackageListCount = "0"; //不能为空，为空，提交接口会报数据格式错误。
            } else {
              data?.PackageListCount = value;
            }
            // isDemandNumUpdate = false;
            // data?.DemandNum = DecimalUtils.multiply(
            //     data?.toDouble(data?.PackageCount).toString(),
            //     data
            //         ?.toDouble(data?.PackageListCount)
            //         .toString()); //fixme 申请数量就等于包数量*每包数量
            // data?.MinNum = data?.DemandNum;
            // NumberOfApplications?.call(() {}); //申请数量
            ///sizeSetState?.call(() {}); //规格更新
            // getStockNum(
            //     context, CurrentInventorySetState, MaterialCodeSetState, data);

            ///库存数量
          });
          return PackageListCount;
        });
        rowChildren.add(PackageListCount);
      }
      //申请数量
      var NumberOfApplications2 = StatefulBuilder(
        builder: (contxet, setState) {
          return UiBase.itemWithSigleEdit(
            context,
            StringUtils.toStringAsDecimal(data?.MinNum),
            isAllowEmpty: true,
            isDecimal: true,
            readOnly: true,
            unit: data?.MainUnit ?? "",
            isNegative: false,
          );
        },
      );
      rowChildren.add(NumberOfApplications2);
      //已采购数量
      var NumberOfApplications3 = StatefulBuilder(
        builder: (contxet, setState) {
          return UiBase.itemWithSigleEdit(
              context, StringUtils.toStringAsDecimal(data?.Quantity),
              isAllowEmpty: true,
              isDecimal: true,
              readOnly: true,
              isNegative: false);
        },
      );
      rowChildren.add(NumberOfApplications3);

      if (firstData.isSubsidiaryNum()) {
        //面积
        var StockArea = StatefulBuilder(
          builder: (contxet, setState) {
            return _text("${data?.SubsidiaryNum}${data?.SubsidiaryUnit}");
          },
        );
        rowChildren.add(StockArea);
      }

      if (isPrice) {
        //单价
        var Price = StatefulBuilder(
          builder: (contxet, setState) {
            // return _text("${data?.Price}");
            var one = _text("${data?.Price}");
            if (data.isChange()) {
              var isSame = StringUtils.isSame(data?.Price, data?.ChangePrice);
              if (!isSame) {
                //fixme 不相同
                var two =
                    _text("${data?.ChangePrice}", isRed: true); //最新的，用红色表示。
                return Column(
                  children: [one, two],
                  mainAxisAlignment: MainAxisAlignment.center,
                );
              }
            }
            return one;
          },
        );
        rowChildren.add(Price);
      }

      if (isAmount) {
        //金额
        var Amount = StatefulBuilder(
          builder: (contxet, setState) {
            return _text("${StringUtils.toStringAsDecimal(data?.Amount)}");
          },
        );
        rowChildren.add(Amount);
      }
      if (isQualityGrade) {
        //质量等级
        var QualityGrade = StatefulBuilder(
          builder: (contxet, setState) {
            var one = _text("${data?.QualityGradeName}");
            if (data.isChange()) {
              var isSame = StringUtils.isSame(
                  data?.QualityGrade, data?.ChangeQualityGrade);
              if (!isSame) {
                //fixme 不相同
                var two = _text("${data?.ChangeQualityGradeName}",
                    isRed: true); //最新的，用红色表示。
                return Column(
                  children: [one, two],
                  mainAxisAlignment: MainAxisAlignment.center,
                );
              }
            }
            return one;
          },
        );
        rowChildren.add(QualityGrade);
      }
      //品牌
      var ManufacturerName = StatefulBuilder(
        builder: (contxet, setState) {
          // return _text(data?.ManufacturerName);
          var one = _text("${data?.ManufacturerName}");
          if (data.isChange()) {
            var isSame = StringUtils.isSame(
                data?.ManufacturerId, data?.ChangeManufacturerId);
            if (!isSame) {
              //fixme 不相同
              var two = _text("${data?.ChangeManufacturerName}",
                  isRed: true); //最新的，用红色表示。
              return Column(
                children: [one, two],
                mainAxisAlignment: MainAxisAlignment.center,
              );
            }
          }
          return one;
        },
      );
      rowChildren.add(ManufacturerName);

      ///当前库存
      var CurrentInventory = StatefulBuilder(
        builder: (contxet, setState) {
          var CurrentInventory = _text("${data?.StockNum}${data?.MainUnit}");
          return CurrentInventory;
        },
      );
      rowChildren.add(CurrentInventory);
      //备注
      var ListBeizhu = _text("${data?.ListBeizhu}");
      rowChildren.add(ListBeizhu);

      ///请购部门
      var DemandDeptName = StatefulBuilder(
        builder: (contxet, setState) {
          var CurrentInventory = _text(data?.DemandDeptName);
          return CurrentInventory;
        },
      );
      rowChildren.add(DemandDeptName);

      ///请购人
      var DemandPersonName = StatefulBuilder(
        builder: (contxet, setState) {
          var CurrentInventory = _text(data?.DemandPersonName);
          return CurrentInventory;
        },
      );
      rowChildren.add(DemandPersonName);
      //请购日志
      var Operation =
          _materialButton(AppStrings.of(context).ToView, onPressed: () {
        //查看
        if (StringUtils.isEmpty(data.WFLogList)) {
          AppDialog.showDialogWithInfo(context,
              content: AppStrings.of(context).RLogEmpty); //暂无请购日志
        } else {
          AppRouteHelper.goPurOrderAuditDetailsLogRoute(
              context, item, data.WFLogList);
        }
      });
      rowChildren.add(Operation);
      var tableRow = TableRow(
          // decoration: BoxDecoration(color: _getColor(_index + 1)),
          decoration: BoxDecoration(color: _getColor(i)),
          children: rowChildren);
      children.add(tableRow);
    }
    //第二行；合计
    var _b = true; //是否合计
    if (_b) {
      var PDMinNum = 0.0; //请购数量
      var MinNum = 0.0; //申请数量
      var Quantity = 0.0; //已采购数量
      var SubsidiaryNum = 0.0; //面积
      var Amount = 0.0; //金额
      var StockNum = 0.0; //当前库存

      var PDMinNumStr = ""; //请购数量
      var SubsidiaryNumStr = ""; //面积
      var AmountStr = ""; //金额
      var _Numbers = 0.0; //数量
      datas?.forEach((element) {
        try {
          var data = element.PDMinNum;
          if (StringUtils.isNotEmpty(data)) {
            PDMinNum = AcombinedUtils.add(PDMinNum, data);
          }
        } catch (e) {}
        try {
          var data = element.MinNum;
          if (StringUtils.isNotEmpty(data)) {
            MinNum = AcombinedUtils.add(MinNum, data);
          }
        } catch (e) {}
        try {
          var data = element.Quantity;
          if (StringUtils.isNotEmpty(data)) {
            Quantity = AcombinedUtils.add(Quantity, data);
          }
        } catch (e) {}
        try {
          var data = element.SubsidiaryNum;
          if (StringUtils.isNotEmpty(data)) {
            SubsidiaryNum = AcombinedUtils.add(SubsidiaryNum, data);
          }
        } catch (e) {}
        try {
          var data = element.Amount;
          if (StringUtils.isNotEmpty(data)) {
            Amount = AcombinedUtils.add(Amount, data);
          }
        } catch (e) {}
        try {
          var data = element.StockNum;
          if (StringUtils.isNotEmpty(data)) {
            StockNum = AcombinedUtils.add(StockNum, data);
          }
        } catch (e) {}
        try {
          var data = element.Numbers;
          if (StringUtils.isNotEmpty(data)) {
            _Numbers = AcombinedUtils.add(_Numbers, data);
          }
        } catch (e) {}
      });
      PDMinNumStr = NumUtil.strToDoubleStr(PDMinNum.toString());
      var MinNumStr = NumUtil.strToDoubleStr(MinNum.toString());
      var QuantityStr = NumUtil.strToDoubleStr(Quantity.toString());
      SubsidiaryNumStr = NumUtil.strToDoubleStr(SubsidiaryNum.toString());
      AmountStr = NumUtil.strToDoubleStr(Amount.toString());
      var StockNumStr = NumUtil.strToDoubleStr(StockNum.toString());
      var _NumbersStr = NumUtil.strToDoubleStr(_Numbers.toString());
      List<Widget> titleChildren = [];
      titleChildren.add(_text(AppStrings.of(context).ACombined)); //fixme 序号；合计
      titleChildren.add(_text("")); //物料名称
      titleChildren.add(_text("")); //物料类型
      titleChildren.add(_text("")); //物料编码
      titleChildren.add(_text("")); //规格型号
      if (firstData.Iswidth2()) {
        titleChildren.add(_text("")); //宽
      }
      if (firstData.Isheight2()) {
        titleChildren.add(_text("")); //高
      }
      if (firstData.Islength2()) {
        titleChildren.add(_text("")); //长
      }
      if (firstData.Isthickness2()) {
        titleChildren.add(_text("")); //厚
      }
      if (firstData.Isquantity2()) {
        titleChildren.add(_text("$_NumbersStr")); //fixme 数量
      }
      if (isSourceType) {
        titleChildren.add(_text("")); //来源类型
      }
      titleChildren.add(_text("$PDMinNumStr", isStart: false)); //fixme 请购数量
      if (!firstData.isMT01()) {
        //辅料，五金；显示；包数量，和每包数量。
        titleChildren.add(_text("", isStart: false)); //包数量
        titleChildren.add(_text("", isStart: false)); //每包数量
      }
      titleChildren.add(_text("${MinNumStr}", isStart: false)); //fixme 申请数量*
      titleChildren.add(_text("${QuantityStr}", isStart: false)); //fixme 已采购数量
      if (firstData.isSubsidiaryNum()) {
        titleChildren.add(_text("${SubsidiaryNumStr}")); //fixme 面积
      }
      if (isPrice) {
        titleChildren.add(_text("", isStart: false)); //单价
      }
      if (isAmount) {
        titleChildren.add(_text("${AmountStr}")); //fixme 金额
      }
      if (isQualityGrade) {
        titleChildren.add(_text("")); //质量等级
      }
      if (firstData.isMT01()) {
        titleChildren.add(_text("")); //品牌
      } else {
        titleChildren.add(_text("", isStart: false)); //品牌(带星)
      }
      titleChildren.add(_text("$StockNumStr")); //fixme 当前库存
      titleChildren.add(_text("")); //备注
      titleChildren.add(_text("")); //请购部门
      titleChildren.add(_text("")); //请购人
      titleChildren.add(_text("")); //请购日志
      var tableRow = TableRow(
          decoration: BoxDecoration(color: _backgroundColorWithTableTitle),
          children: titleChildren);
      children.add(tableRow);
    }
    //表格
    var table = Table(
      border: TableBorder.all(
          color: _BorderColor, width: _width, style: BorderStyle.solid),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: IntrinsicColumnWidth(),
        1: IntrinsicColumnWidth(),
        2: IntrinsicColumnWidth(),
        3: IntrinsicColumnWidth(),
        4: IntrinsicColumnWidth(),
        5: IntrinsicColumnWidth(),
        6: IntrinsicColumnWidth(),
        7: IntrinsicColumnWidth(),
        8: IntrinsicColumnWidth(),
        9: IntrinsicColumnWidth(),
        10: IntrinsicColumnWidth(),
        11: IntrinsicColumnWidth(),
        12: IntrinsicColumnWidth(),
        13: IntrinsicColumnWidth(),
        14: IntrinsicColumnWidth(),
        15: IntrinsicColumnWidth(),
        16: IntrinsicColumnWidth(),
        17: IntrinsicColumnWidth(),
        18: IntrinsicColumnWidth(),
        19: IntrinsicColumnWidth(),
        20: IntrinsicColumnWidth(),
        21: IntrinsicColumnWidth(),
        22: IntrinsicColumnWidth(),
        23: IntrinsicColumnWidth(),
      },
      children: children,
    );
    return table;
  }

  ///获取库存数量
  static getStockNum(BuildContext context,
      StateSetter? CurrentInventorySetState, PurDemand data) {
    ///data?.StockNum
    MaterialModel.getStockNum(context, data, false, (value) {
      if (value.isSuccessForCode()) {
        CurrentInventorySetState?.call(() {
          data?.StockNum = value.StockNum;
          //print("物料请购库存：${data?.StockNum}");
        });
      }
    });
  }

  //fixme 条目颜色背景
  static Color _getColor(int i) {
    Color _color = _backgroundColorWithWhite;
    //print("=============$i ${i%2}");
    if (i % 2 == 0) {
      _color = _backgroundColorWithGrey;
    }
    return _color;
  }

  //文本
  static Widget _text(String? text,
      {bool isStart = false, bool isRed = false}) {
    // return Center(
    //   child: Text(text ?? ""),
    // );
    Widget child;
    var textStyle = AppStyle.textStyleWithCard();
    if (isRed) {
      textStyle = AppStyle.textStyleWithCardRed();
    }
    if (isStart) {
      var textKey = Text(
        text ?? "",
        style: textStyle,
      );
      var value = "*";
      var startText = Text(
        value,
        softWrap: true,
        style: AppStyle.textStyleWithCardRed(),
      );
      child = SizedBox(
        child: Row(
          children: [textKey, startText],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
        width: 120,
      );
    } else {
      child = Text(
        text ?? "",
        style: textStyle,
      );
    }
    return Center(
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minHeight: 36.0, //最小高度
        ),
        child: Center(
          child: child,
        ),
      ),
    );
  }

  //复选框
  //isChecked 是否选中
  //isEnable 是否可操作
  //onChanged 选中回调
  static Widget _checkbox(bool isChecked,
      {bool isEnable = true, Function(bool)? onChanged}) {
    Color activeColor = Colors.blueAccent;
    Color inactiveColor = Colors.black;
    if (!isEnable) {
      //不可用，颜色变暗。
      activeColor = Colors.black12;
      inactiveColor = Colors.black12;
    }
    return Container(
      alignment: Alignment.center,
      // height: 30,
      // width: 30,
      padding: EdgeInsets.zero,
      margin: EdgeInsets.zero,
      child: CircleCheckbox(
        isEnable: isEnable,
        value: isChecked,
        //value 是否选中
        width: 20,
        //width 宽度
        height: 20,
        //height 高度
        activeColor: activeColor,
        //activeColor 选中时背景颜色
        inactiveColor: inactiveColor,
        //inactiveColor 未选中时颜色
        style: CircleCheckboxStyle.tick_hollow,
        //circle_point 圆点样式
        //checked: , 自定义选中组件；fixme 优先级比以上属性设置要高。
        //unChecked: ,自定义未选中组件
        onChanged: (bool) {
          //选中状态改变时回调，如点击时。
          //选中状态改变时回调。
          //setState(() {});
          if (isEnable) {
            onChanged?.call(bool);
          }
        },
      ),
    );
  }

  ///按钮
  static Widget _materialButton(String text,
      {VoidCallback? onPressed,
      double radius = 5,
      double height = 35,
      double width = 50,
      Color color = Colors.blueAccent,
      Color textColor = Colors.white,
      double fontSize = 14,
      EdgeInsetsGeometry margin = const EdgeInsets.only(left: 8, right: 8),
      EdgeInsetsGeometry padding =
          const EdgeInsets.only(left: 16, right: 16)}) {
    return Container(
      margin: margin,
      child: MaterialButton(
        padding: padding,
        height: height,
        minWidth: width,
        onPressed: () {
          onPressed?.call();
        },
        //label 右边控件（一般为文本），不能为空
        color: color,
        //color 按钮颜色
        splashColor: Colors.black12,
        //splashColor 点击时，飞溅扩散的颜色
        //highlightColor: Colors.red, //highlightColor 点击时按钮颜色
        colorBrightness: Brightness.light,
        //colorBrightness 按钮亮度；Brightness.dark 暗系风格;Brightness.light 高亮(默认)
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(radius))),
        //shape 形状
        textColor: Colors.white,
        //文本默认颜色
        child: Text(
          text,
          style: TextStyle(
            color: textColor,
            fontSize: fontSize,
          ),
        ), //fixme 子控件
      ),
    );
  }
}
